#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_ClearDiagInfo_0x14_test.py
@Time    :   2022/08/27 13:31:28
@Author  :   YangLiang
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume
import Set_up
import pytest
import time
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True


ClearDtcInfo_NRC13 = [([0x14],                         [0x7F, 0x14, 0x13]),
                      ([0x14, 0xFF],                   [0x7F, 0x14, 0x13]),
                      ([0x14, 0xFF, 0xFF],             [0x7F, 0x14, 0x13]),
                      ([0x14, 0xFF, 0xFF, 0xFF, 0xFF], [0x7F, 0x14, 0x13]),
                      ([0x14, 0x11, 0x22, 0xFF, 0xFF], [0x7F, 0x14, 0x13]),
                      ([0x14, 0x8C, 0x7A],             [0x7F, 0x14, 0x13]),
                      ]

ClearDtcInfo_NRC31 = [([0x14, 0xFF, 0xFF, 0xFE], [0x7F, 0x14, 0x31]),
                      ([0x14, 0x8C, 0x7A, 0x17], [0x7F, 0x14, 0x31]),
                      ([0x14, 0x98, 0x8C, 0x01], [0x7F, 0x14, 0x31]),
                      ([0x14, 0x98, 0x8C, 0x79], [0x7F, 0x14, 0x31]),
                      ([0x14, 0x98, 0x8A, 0x96], [0x7F, 0x14, 0x31]),
                      ([0x14, 0xF2, 0x42, 0x87], [0x7F, 0x14, 0x31]),
                      ([0x14, 0xF2, 0x43, 0x88], [0x7F, 0x14, 0x31]),
                      ([0x14, 0xF2, 0x40, 0x88], [0x7F, 0x14, 0x31]),
                      ([0x14, 0xD7, 0x61, 0x87], [0x7F, 0x14, 0x31]),
                      ([0x14, 0xD7, 0x66, 0x87], [0x7F, 0x14, 0x31]),
                      ([0x14, 0xD7, 0x70, 0x87], [0x7F, 0x14, 0x31]),
                      ([0x14, 0xD7, 0x71, 0x87], [0x7F, 0x14, 0x31]),
                      ([0x14, 0xD7, 0x72, 0x87], [0x7F, 0x14, 0x31])
                      ]


class Test_ClearDiagInfo_Phy(object):
    @allure.severity('normal')
    @allure.story('Clear Diagnostic information Test in default session with physical addressing')
    @allure.description('Test positive response of 0x14 in default session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_ClearDiagInfo_Default_Phy(self, action, Driver):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 1, iStep, False)

    @allure.severity('normal')
    @allure.description('Test positive response of 0x14 in extended session with physical addressing')
    def test_ClearDiagInfo_Extended_Phy(self, action, Driver):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 3, iStep, False)

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in ClearDtcInfo_NRC13]
    @allure.severity('minor')
    @allure.description('Test NRC13 of 0x14 in default session with physical addressing')
    @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC13, ids=ids)
    def test_ClearDiagInfo_NRC13_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in ClearDtcInfo_NRC13]
    @allure.severity('minor')
    @allure.description('Test NRC13 of 0x14 in extended session with physical addressing')
    @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC13, ids=ids)
    def test_ClearDiagInfo_NRC13_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in ClearDtcInfo_NRC31]
    @allure.severity('minor')
    @allure.description('Test NRC31 of 0x14 in default session with physical addressing')
    @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC31, ids=ids)
    def test_ClearDiagInfo_NRC31_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in ClearDtcInfo_NRC31]
    @allure.severity('minor')
    @allure.description('Test NRC31 of 0x14 in extended session with physical addressing')
    @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC31, ids=ids)
    def test_ClearDiagInfo_NRC31_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

    @allure.severity('minor')
    @allure.description('Test NRC22 of 0x14 in default session with physical addressing')
    def test_ClearDiagInfo_NRC22_Default_Phy(self, action, Driver):
        iStep = 1
        # 设置电压为17v，并等待1.5s
        iStep = Driver['CAN'].SetVolt(Driver, 17, 1.5, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x7F, 0x14, 0x22], 3, iStep, False)
        # 设置电压为12v，并等待1.5s
        iStep = Driver['CAN'].SetVolt(Driver, 12, 0.5, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 0, iStep, False)
    
    @allure.severity('minor')
    @allure.description('Test NRC22 of 0x14 in extended session with physical addressing')
    def test_ClearDiagInfo_NRC22_Extended_Phy(self, action, Driver):
        iStep = 1
        # 设置车速>3kph
        Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'PBM_0x185', 'VehSpd', 80)
        time.sleep(2)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x7F, 0x14, 0x22], 3, iStep, False)
        # 设置车速<3kph
        Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'PBM_0x185', 'VehSpd', 0)
        time.sleep(0.5)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 0, iStep, False)

    
    @allure.severity('minor')
    @allure.description('Test NRC22 of 0x14 in default session with physical addressing')
    def test_ClearDiagInfo_NRC_Priority_Default_Phy(self, action, Driver):
        iStep = 1
        # 设置电压为17v，并等待1.5s
        iStep = Driver['CAN'].SetVolt(Driver, 17, 1.5, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF], [0x7F, 0x14, 0x13], 3, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF,0xFE], [0x7F, 0x14, 0x31], 0, iStep, False)
        # 设置电压为12v，并等待1.5s
        iStep = Driver['CAN'].SetVolt(Driver, 12, 0.5, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 0, iStep, False)



class Test_ClearDiagInfo_Fun(object):
    @allure.severity('normal')
    @allure.story('Clear Diagnostic information Test in default session with functional addressing')
    @allure.description('Test positive response of 0x14 in default session with functional addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_ClearDiagInfo_Default_Fun(self, action, Driver):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 1, iStep, False, 'UDSFun')

    @allure.severity('normal')
    @allure.description('Test positive response of 0x14 in extended session with functional addressing')
    def test_ClearDiagInfo_Extended_Fun(self, action, Driver):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 3, iStep, False, 'UDSFun')

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in ClearDtcInfo_NRC13]
    @allure.severity('minor')
    @allure.description('Test NRC13 of 0x14 in default session with functional addressing')
    @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC13, ids=ids)
    def test_ClearDiagInfo_NRC13_Default_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun')

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in ClearDtcInfo_NRC13]
    @allure.severity('minor')
    @allure.description('Test NRC13 of 0x14 in extended session with functional addressing')
    @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC13, ids=ids)
    def test_ClearDiagInfo_NRC13_Extended_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun')

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in ClearDtcInfo_NRC31]
    @allure.severity('minor')
    @allure.description('Test NRC31 of 0x14 in default session with functional addressing')
    @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC31, ids=ids)
    def test_ClearDiagInfo_NRC31_Default_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 1, iStep, False, 'UDSFun')

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in ClearDtcInfo_NRC31]
    @allure.severity('minor')
    @allure.description('Test NRC31 of 0x14 in extended session with functional addressing')
    @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC31, ids=ids)
    def test_ClearDiagInfo_NRC31_Extended_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 3, iStep, False, 'UDSFun')

    @allure.severity('minor')
    @allure.description('Test NRC22 of 0x14 in default session with functional addressing')
    def test_ClearDiagInfo_NRC22_Default_Fun(self, action, Driver):
        iStep = 1
        # 设置电压为17v，并等待1.5s
        iStep = Driver['CAN'].SetVolt(Driver, 17, 1.5, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x7F, 0x14, 0x22], 3, iStep, False, 'UDSFun')
        # 设置电压为12v，并等待1.5s
        iStep = Driver['CAN'].SetVolt(Driver, 12, 0.5, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 0, iStep, False, 'UDSFun')
    
    @allure.severity('minor')
    @allure.description('Test NRC22 of 0x14 in extended session with functional addressing')
    def test_ClearDiagInfo_NRC22_Extended_Fun(self, action, Driver):
        iStep = 1
        # 设置车速>3kph
        Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'PBM_0x185', 'VehSpd', 80)
        time.sleep(2)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x7F, 0x14, 0x22], 3, iStep, False, 'UDSFun')
        # 设置车速<3kph
        Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'PBM_0x185', 'VehSpd', 0)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 0, iStep, False, 'UDSFun')

    
    @allure.severity('minor')
    @allure.description('Test NRC22 of 0x14 in default session with functional addressing')
    def test_ClearDiagInfo_NRC_Priority_Default_Fun(self, action, Driver):
        iStep = 1
        # 设置电压为17v，并等待1.5s
        iStep = Driver['CAN'].SetVolt(Driver, 17, 1.5, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF], [0x7F, 0x14, 0x13], 3, iStep, False, 'UDSFun')
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF,0xFE], [], 0, iStep, False, 'UDSFun')
        # 设置电压为12v，并等待1.5s
        iStep = Driver['CAN'].SetVolt(Driver, 12, 0.5, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 0, iStep, False, 'UDSFun')